var express = require('express');
var router = express.Router();
const myQuery = require('../tools/db');
const {apiUrl}=require('../tools/config');


//banner接口
router.get('/banner', async (req, res, next) => {
  let [err, result] = await myQuery(`SELECT CONCAT('${apiUrl}',image_src) AS image_src,title from e_banners where isshow=1;`);
  if (err) {
    next({ code: 500, msg: err })
  } else {
    res.send({ code: 200, msg: 'ok', result });
  }
});


//online课程接口
router.get('/online',async(req,res,next)=>{
  let sql=`SELECT CONCAT('${apiUrl}','course/',image_src) AS image_src,title,cid,price,type_name,intro,grade_name,area_name from e_course where type=1 LIMIT 0,8;`;
  let [err,result]=await myQuery(sql);
  if(err){
    next({code:500,msg:err});
    return;
  }else{
    res.send({code:200,msg:'ok',result});
  }
})

//test课程接口
router.get('/test',async(req,res,next)=>{
  let sql=`SELECT CONCAT('${apiUrl}','/course/',image_src) AS image_src,title,cid,price,type_name,intro,grade_name,area_name from e_course where type=2 LIMIT 0,6;`;
  let [err,result]=await myQuery(sql);
  if(err){
    next({code:500,msg:err});
    return;
  }else{
    res.send({code:200,msg:'ok',result});
  }
})

//good课程接口
router.get('/good',async(req,res,next)=>{
  let sql=`SELECT CONCAT('${apiUrl}','/course/',image_src) AS image_src,title,cid,price,type_name,intro,grade_name,area_name from e_course where type=1 LIMIT 0,6;`;
  let [err,result]=await myQuery(sql);
  if(err){
    next({code:500,msg:err});
    return;
  }else{
    res.send({code:200,msg:'ok',result});
  }
})


module.exports = router;
